package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.pojo.Vote;
import com.mls.fivedistrict.pojo.VoteOption;
import com.mls.fivedistrict.server.VoteOptionService;
import com.mls.fivedistrict.server.VoteService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 投票选项 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-05-21
 */
@RestController
@RequestMapping("/api/manager")
public class VoteOptionController {
    @Autowired
    private VoteOptionService voteOptionService;

    @RequestMapping("/v1/voteOptions")
    public ServiceResponse getList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<VoteOption> voteOptionList = voteOptionService.selectListAndVoteOption(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(voteOptionList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }
    @RequestMapping("/v1/voteOption/all")
    public ServiceResponse getAll() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        List<VoteOption> voteOptionList = voteOptionService.selectList(pageFilter.getWrapper());
        return ServiceResponse.createSuccessResponse(voteOptionList);
    }

    @GetMapping("/v1/voteOption/{voteOptionId}")
    public ServiceResponse getVoteOption(@PathVariable("voteOptionId") Long voteOptionId) {
//        VoteOption voteOption = voteOptionService.selectById(voteOptionId);
        VoteOption voteOption = voteOptionService.selectAndVoteById(voteOptionId);
        return ServiceResponse.createSuccessResponse(voteOption);
    }


    @PostMapping("/v1/voteOption")
    public ServiceResponse insert(@RequestBody VoteOption voteOption) {
        BeanValidator.check(voteOption);
        boolean insert = voteOptionService.insert(voteOption);
        if (!insert) {
            return ServiceResponse.createErrorResponse("插入失败");
        }
        return ServiceResponse.createSuccessResponse("插入成功");
    }

    @PutMapping("/v1/voteOption/{voteOptionId}")
    public ServiceResponse update(@PathVariable("voteOptionId") Long voteOptionId, @RequestBody VoteOption voteOption) {
        voteOption.setId(voteOptionId);
        boolean update = voteOptionService.updateById(voteOption);
        if (!update) {
            return ServiceResponse.createErrorResponse("修改失败");
        }
        return ServiceResponse.createSuccessResponse("修改成功");
    }

    @DeleteMapping("/v1/voteOption/{voteOptionId}")
    public ServiceResponse delete(@PathVariable("voteOptionId") Long voteOptionId) {
        VoteOption voteOption = new VoteOption();
        voteOption.setId(voteOptionId);
        voteOption.setIsDelete(true);
        boolean delete = voteOptionService.updateById(voteOption);
        if (!delete) {
            return ServiceResponse.createErrorResponse("删除失败");
        }
        return ServiceResponse.createSuccessResponse("删除成功");
    }
}

